package com.aisino.inveli.vinda.service.impl;

import com.aisino.inveli.vinda.entity.*;
import com.aisino.inveli.vinda.service.InvEliService;
import com.aisino.inveli.vinda.utils.ExcelUtils;
import com.aisinogd.fp.dzfp.companyinterface.hessian.pojo.*;
import com.aisinogd.fp.dzfp.companyinterface.webservice.IEliWebService;
import com.aisinogd.fp.dzfp.companyinterface.webservice.IEliWebServicePortType;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author leifeng
 * @Date 2017-11-17
 **/
@Service
public class InvEliServiceImpl implements InvEliService {

    @Override
    public InvEliInfo packageBean(GoodsList sysGoodList, ConfigEntity sysConfigEntity) {
        InvEliInfo info = new InvEliInfo();
        String[] header = ExcelUtils.getHeaderArray(sysGoodList.getHeaderStr());
        //设置发票信息 -- 唯一流水号
        info.setFPQQLSH(header[0]);
        //设置发票信息 -- 订单号
        info.setDDH(header[0]);
        //设置开票方信息 -- 开票方税号
        info.setKP_NSRSBH(sysConfigEntity.getRegNumber());
        //设置开票方名称
        info.setKP_NSRMC(sysConfigEntity.getSalesName());
        //代开标志
        info.setDKBZ("0");
        //销方信息
        info.setXHF_NSRSBH(sysConfigEntity.getRegNumber());
        info.setXHFMC(sysConfigEntity.getSalesName());
        info.setXHF_DZ(sysConfigEntity.getSalesAddress());
        info.setXHF_DH(sysConfigEntity.getSalesTel());
        info.setXHF_YHZH(sysConfigEntity.getSalesBank());
        /**
        //购方信息
        info.setGHF_MC(header[1]);
        info.setGHF_NSRSBH(header[2]);

        info.setGHF_DZ(header[3]);
        info.setGHF_YHZH(header[4]);
        //info.setGHF_SJ("18826243713");
        info.setGHF_EMAIL("1084855857@qq.com");

        //开票员
        info.setKPR(sysConfigEntity.getHandler());
        info.setSKR(sysConfigEntity.getPayee());
        info.setFHR(sysConfigEntity.getChecker());
        **/
        //开票类型    1正票、2红票
        info.setKPLX("1");
        //操作代码   10-->正常开票    11-->正票错票重开   20--> 退货折让红票  21-->错票重开红票    22-->换票冲红（全冲红电子发票，开具纸质发票）
        info.setCZDM("10");
        List<InvEliDetail> detailList = new ArrayList<>();
        for (int i = 0; i < sysGoodList.getResultZS().size(); i++) {
            InvEliDetail details = new InvEliDetail();
            InvEliDetail disDetails = new InvEliDetail();
            GoodsDetail strs = sysGoodList.getResultZS().get(i);
            //正数商品
            details.setXMMC(strs.getName());
            details.setDW(strs.getDw());
            details.setGGXH(strs.getType());
            String num = strs.getNum();
            details.setXMSL(Double.parseDouble(num));
            //含税金额 -->项目金额
            BigDecimal mon = new BigDecimal(strs.getNewMon() == null ? strs
                    .getMon() : strs.getNewMon());
            Double price = getPrice(mon,num);
            details.setXMDJ(price);
            details.setXMJE(mon.doubleValue());
            details.setSL(new BigDecimal(strs.getRate()).toString());
            details.setSPBM(strs.getSsflbm());
            details.setHSBZ("1");
            //税额
            BigDecimal rateMon = mon.divide(new BigDecimal(1)
                    .add(new BigDecimal(strs.getRate())),2,BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal(strs.getRate())).setScale(2,BigDecimal.ROUND_HALF_UP);
            details.setSE(rateMon.doubleValue());
            //优惠政策标识
            details.setYHZCBS("0");
            // 加入正数商品
            detailList.add(details);
            //折扣金额
            BigDecimal dismon = new BigDecimal(strs.getDisMon()).multiply(new BigDecimal(-1)).setScale(2, BigDecimal.ROUND_HALF_UP);
            if (dismon.compareTo(new BigDecimal(0)) != 0) {
                //正数商品折扣行
                disDetails.setXMMC(strs.getName());
                disDetails.setDW(strs.getDw());
                disDetails.setGGXH(strs.getType());
                disDetails.setXMSL(Double.parseDouble(num));
                disDetails.setXMJE(dismon.doubleValue());
                //折扣商品税额
                BigDecimal disRateMon = dismon.divide(new BigDecimal(1)
                        .add(new BigDecimal(strs.getRate())),2,BigDecimal.ROUND_HALF_UP)
                        .multiply(new BigDecimal(strs.getRate())).setScale(2,BigDecimal.ROUND_HALF_UP);
                disDetails.setSE(disRateMon.doubleValue());
                //折扣商品单价
                disDetails.setXMDJ(dismon.divide(new BigDecimal(num),8,BigDecimal.ROUND_HALF_UP).doubleValue());
                //折扣商品税率
                disDetails.setSL(strs.getRate());
                disDetails.setSPBM(strs.getSsflbm());
                disDetails.setHSBZ("1");
                //优惠政策标识
                disDetails.setYHZCBS("0");
                detailList.add(disDetails);
            }
        }
        info.setKPHJJE(sysGoodList.getOracleTotalMon());
        info.setDetailList(detailList);
        //税收分类编码版本
        info.setBMB_BBH("12.0");
        return info;
    }

    /**
     * 获取商品单价
     * @param mon
     * @param num
     * @return
     */
    private static Double getPrice( BigDecimal mon, String num) {
        Double price = mon.divide(new BigDecimal(num), 8, BigDecimal.ROUND_HALF_UP).doubleValue();
        return price;
    }

    /**
     * 同步开具发票
     */
    public ReturnElectronice InvEli(InvEliInfo info) {
        IEliWebService wb = new IEliWebService();
        // 对象工厂
        ObjectFactory of = new ObjectFactory();
        /******** 发票总体类 ********/
        ElectroniceInfo e = new ElectroniceInfo();
        e.setUserName(of.createElectroniceInfoUserName("14410101"));
        e.setPassWord(of.createElectroniceInfoPassWord("JWVFkqs7IP+++xBb1I9a/qr6/L/qYxyw=="));
        // 发票请求唯一流水号
        e.setFPQQLSH(of.createElectroniceInfoFPQQLSH(info.getFPQQLSH()));
        // 业务订单号
        e.setDDH(of.createElectroniceInfoDDH(info.getDDH()));
        // 开票方纳税人识别号
        e.setKPNSRSBH(of.createElectroniceInfoKPNSRSBH(info.getKP_NSRSBH()));
        // 开票方名称
        e.setKPNSRMC(of.createElectroniceInfoKPNSRMC(info.getKP_NSRMC()));
        // 开票方电子档案号
        e.setNSRDZDAH(of.createElectroniceInfoNSRDZDAH(info.getNSRDZDAH()));
        // 税务机构代码
        e.setSWJGDM(of.createElectroniceInfoSWJGDM(info.getSWJG_DM()));
        // 代开标志
        e.setDKBZ(of.createElectroniceInfoDKBZ(info.getDKBZ()));
        // 主要开票项目
        e.setKPXM(of.createElectroniceInfoKPXM(info.getKPXM()));
        // 销货方识别号
        e.setXHFNSRSBH(of.createElectroniceInfoXHFNSRSBH(info.getXHF_NSRSBH()));
        // 销货方名称
        e.setXHFMC(of.createElectroniceInfoXHFMC(info.getXHFMC()));
        // 销货方地址
        e.setXHFDZ(of.createElectroniceInfoXHFDZ(info.getXHF_DZ()));
        // 销货方电话
        e.setXHFDH(of.createElectroniceInfoXHFDH(info.getXHF_DH()));
        // 销货方银行账号
        e.setXHFYHZH(of.createElectroniceInfoXHFYHZH(info.getXHF_YHZH()));
        // 购货方名称
        e.setGHFMC(of.createElectroniceInfoGHFMC(info.getGHF_MC()));
        // 购货方纳税人识别号
        e.setGHFNSRSBH(of.createElectroniceInfoGHFNSRSBH(info.getGHF_NSRSBH()));
        // 购货方地址
        e.setGHFDZ(of.createElectroniceInfoGHFDZ(info.getGHF_DZ()));
        // 购货方省份
        e.setGHFSF(of.createElectroniceInfoGHFSF(info.getGHF_SF()));
        // 购货方固定电话
        e.setGHFGDDH(of.createElectroniceInfoGHFGDDH(info.getGHF_GDDH()));
        // 购货方手机
        e.setGHFSJ(of.createElectroniceInfoGHFSJ(info.getGHF_SJ()));
        // 购货方邮箱
        e.setGHFEMAIL(of.createElectroniceInfoGHFEMAIL(info.getGHF_EMAIL()));
        // 购货方企业类型
        e.setGHFQYLX(of.createElectroniceInfoGHFQYLX(info.getGHFQYLX()));
        // 购货方银行账号
        e.setGHFYHZH(of.createElectroniceInfoGHFYHZH(info.getGHF_YHZH()));
        // 行业代码
        e.setHYDM(of.createElectroniceInfoHYDM(info.getHY_DM()));
        // 行业名称
        e.setHYMC(of.createElectroniceInfoHYMC(info.getHY_MC()));
        // 开票员
        e.setKPR(of.createElectroniceInfoKPR(info.getKPR()));
        // 收款员
        e.setSKR(of.createElectroniceInfoSKR(info.getSKR()));
        // 复核人
        e.setFHR(of.createElectroniceInfoFHR(info.getFHR()));
        // 开票类型
        e.setKPLX(of.createElectroniceInfoKPLX(info.getKPLX()));
        // 操作代码
        e.setCZDM(of.createElectroniceInfoCZDM(info.getCZDM()));
        // ----------------------------------冲红时使用--------------------------------------------------
        // 原发票代码
        e.setYFPDM(of.createElectroniceInfoYFPDM(info.getYFP_DM()));
        // 原发票号码
        e.setYFPHM(of.createElectroniceInfoYFPHM(info.getYFP_HM()));
        // 特殊冲红标志
        e.setTSCHBZ(of.createElectroniceInfoTSCHBZ(info.getTSCHBZ()));
        // 冲红原因
        e.setCHYY(of.createElectroniceInfoCHYY(info.getCHYY()));
        // 价税合计金额
        e.setKPHJJE(info.getKPHJJE());
        // 合计不含税金额
        e.setHJBHSJE(info.getHJBHSJE());
        // 合计税额
        e.setHJSE(info.getHJSE());
        // 税收分类编码版本
        e.setBMBBBH(of.createElectroniceInfoBMBBBH(info.getBMB_BBH()));
        // 备注
        e.setBZ(of.createElectroniceInfoBZ(info.getBZ()));

        ArrayOfElectroniceDetail list = new ArrayOfElectroniceDetail();
        /******** 发票明细类 ********/
        List<ElectroniceDetail> ds =  list.getElectroniceDetail();
        List<InvEliDetail> detailList = info.getDetailList();
        for (int i = 0; i < detailList.size(); i++) {
            ElectroniceDetail detail = new ElectroniceDetail();
            // 项目名称
            detail.setXMMC(of.createElectroniceDetailXMMC(detailList.get(i).getXMMC()));
            // 项目单位
            detail.setDW(of.createElectroniceDetailDW(detailList.get(i).getDW()));
            // 规格型号
            detail.setGGXH(of.createElectroniceDetailGGXH(detailList.get(i).getGGXH()));
            // 项目数量
            detail.setXMSL(detailList.get(i).getXMSL());
            // 含税标志
            detail.setHSBZ(of.createElectroniceDetailHSBZ(detailList.get(i).getHSBZ()));
            // 项目单价
            detail.setXMDJ(detailList.get(i).getXMDJ());
            // 项目编码
            detail.setXMBM(of.createElectroniceDetailXMBM(detailList.get(i).getXMBM()));
            // 商品分类编码
            detail.setSPBM(of.createElectroniceDetailSPBM(detailList.get(i).getSPBM()));
            // 自行编码
            detail.setZXBM(of.createElectroniceDetailZXBM(detailList.get(i).getZXBM()));
            // 优惠政策标识
            detail.setYHZCBS(of.createElectroniceDetailYHZCBS(detailList.get(i).getYHZCBS()));
            // 零税率标识
            detail.setLSLBS(of.createElectroniceDetailLSLBS(detailList.get(i).getLSLBS()));
            // 增值税特殊管理
            detail.setZZSTSGL(of.createElectroniceDetailZZSTSGL(detailList.get(i).getZZSTSGL()));
            // 项目金额
            detail.setXMJE(detailList.get(i).getXMJE());
            // 税率
            detail.setSL(of.createElectroniceDetailSL(detailList.get(i).getSL()));
            // 项目税额
            detail.setSE(detailList.get(i).getSE());

            ds.add(detail);
        }
        e.setDetails(of.createElectroniceInfoDetails(list));
        ReturnElectronice result = wb.getIEliWebServiceHttpPort().invEli(e);
        return result;
    }

    /**
     * 发票库存查询
     */
    public ElectroniceStock queryEliStock(String KP_NSRSH) {
        IEliWebService factory = new IEliWebService();
        IEliWebServicePortType iEliWebService = factory.getIEliWebServiceHttpPort();
        ElectroniceStock result = iEliWebService.queryEliStock(KP_NSRSH);
        return result;
    }

    /**
     * 查询电子发票信息
     */
    public ReturnElectronice queryEliData(InvEliInfo info) {
        IEliWebService factory = new IEliWebService();
        IEliWebServicePortType iEliWebService = factory.getIEliWebServiceHttpPort();
        // 对象工厂
        ObjectFactory of = new ObjectFactory();
        /******** 发票总体类 ********/
        ElectroniceInfo e = new ElectroniceInfo();
        // 发票请求唯一流水号
        e.setFPQQLSH(of.createElectroniceInfoFPQQLSH(info.getFPQQLSH()));
        // 订单号
        e.setDDH(of.createElectroniceInfoDDH(info.getDDH()));
        // 开票方纳税人识别号
        e.setKPNSRSBH(of.createElectroniceInfoGHFNSRSBH(info.getKP_NSRSBH()));
        // 销货方识别号
        e.setXHFNSRSBH(of.createElectroniceInfoXHFNSRSBH(info.getXHF_NSRSBH()));
        ReturnElectronice result = iEliWebService.queryEliData(e);
        return result;
    }

}
